#include "stm32f10x.h"   
#include "Delay.h"
#include "led.h"
#include "Buzzer.h"
#include "sys.h"
extern u8 alarm_is_free;
/*按键初始化*/
void Key_Init(void)
{
	/*rcc配置*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	
	/*GPIO配置*/
	GPIO_InitTypeDef GPIO_Initstructure;
	GPIO_Initstructure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_Initstructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_11;
	GPIO_Initstructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_Initstructure);
	
	/*AFIO*/
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1); //配置AFIO数据选择器
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource11);
	
	/*配置exti*/
	EXTI_InitTypeDef EXTI_Initstructure;
	EXTI_Initstructure.EXTI_Line=EXTI_Line1|EXTI_Line11;
	EXTI_Initstructure.EXTI_Mode=EXTI_Mode_Interrupt;
	EXTI_Initstructure.EXTI_Trigger=EXTI_Trigger_Falling;
	EXTI_Initstructure.EXTI_LineCmd=ENABLE;
	EXTI_Init(&EXTI_Initstructure);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	/*配置NVIC*/
	NVIC_InitTypeDef NVIC_Initstructure;
	NVIC_Initstructure.NVIC_IRQChannel=EXTI1_IRQn;
	NVIC_Initstructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Initstructure.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_Initstructure.NVIC_IRQChannelSubPriority=1;
	NVIC_Init(&NVIC_Initstructure);	
	
	NVIC_Initstructure.NVIC_IRQChannel=EXTI15_10_IRQn;
	NVIC_Initstructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Initstructure.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_Initstructure.NVIC_IRQChannelSubPriority=2;
	NVIC_Init(&NVIC_Initstructure);	
}

/*按键检测*/
uint8_t Key_GetNum(void)
{
	uint8_t Key_GetNum=0;
	if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)   
	{
		Delay_ms(20);
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)
		Delay_ms(20);
		Key_GetNum=1;
	}
	
	if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)
	{
		Delay_ms(20);
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)
		Delay_ms(20);
		Key_GetNum=2;
	}
	
	return Key_GetNum;
}

/*中断函数*/
void EXTI1_IRQHandler(void)
{	
	if(EXTI_GetITStatus(EXTI_Line1)==SET)
	{
		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)
		{	
			if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)
			{
				LED1=!LED1;
			}
			}
			
		}			
		EXTI_ClearITPendingBit(EXTI_Line1);		
	}

void EXTI15_10_IRQHandler(void)
{ 
	if(EXTI_GetITStatus(EXTI_Line11)==SET)
	{
		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)
		{	
			if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)
			{	
				alarmFlag=!alarmFlag;
				alarm_is_free=0;
			}
			}
		}	
		EXTI_ClearITPendingBit(EXTI_Line11);
}


